#!/usr/bin/env python
#
#       Particles.py
#       
#       Copyright 2009 Robin Hayes <bonzairob@hotmail.co.uk>
#       
#       This program is free software; you can redistribute it and/or modify
#       it under the terms of the GNU General Public License as published by
#       the Free Software Foundation; either version 2 of the License, or
#       (at your option) any later version.
#       
#       This program is distributed in the hope that it will be useful,
#       but WITHOUT ANY WARRANTY; without even the implied warranty of
#       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#       GNU General Public License for more details.
#       
#       You should have received a copy of the GNU General Public License
#       along with this program; if not, write to the Free Software
#       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#       MA 02110-1301, USA.

import math
import os
import pygame
import random

import Graphics
import Options

from Client import Client
client = Client()


'''Probably need to load the images within init?
But that stops us changing it so, maybe not. Maybe on load.
need to subclass I think.'''


class Particles(list):
    def __init__(self):
        pass
        list.__init__(self)
        self.graphics = Graphics.Graphics()
        #I CANNOT HANDLE THE PARTICLOLS
        #self.displayed = Options.PARTICLES
        #0 for none, 1 for all, fine tuned with other numbers.
        

    
class Smoke(Particles):
    #(300, 200, "smoke1-", 4, 10, 10, 100, >1)
    def __init__(self, x, y, icle, frames, x_speed, y_speed, lifetime, strength):
        Particles.__init__(self)
        self.x = x
        self.y = y
        self.frames = frames
        self.icles = []
        self.x_speed = x_speed
        self.y_speed = y_speed
        self.strength = strength
        
        self.spread = 0.3;
        self.lifetime = lifetime
        self.iclesf = []
        self.reversex, self.reversey = False, False
        if self.x_speed < 0:
            self.reversex = True
        if self.y_speed < 0:
            self.reversey = True
        
        for a in range(0, self.frames):
            licle = icle + str(a) + ".png"
            self.iclesf.append(self.graphics.__load__(os.path.join("Data", "Particles", licle)).convert_alpha()) # image, rect)
        
    def create(self):
        #print "Creating"
        self.icles.append([])
        self.icles[-1].append(self.x)
        self.icles[-1].append(self.y)
        self.icles[-1].append((math.pi/2) * 3 + random.uniform(-self.spread, self.spread))
        self.icles[-1].append( random.randint( self.lifetime, round(self.lifetime + (self.lifetime/10) ) ) )
        self.icles[-1].append(random.randint(0, self.frames-1))
        #x, y, angle, lifetime, frame
        
    def destroy(self):
        #print "Destroying"
        self.icles.remove(self.icles[0])
        
    def changewind(self, x, y, strength):
        self.x_speed = x
        self.y_speed = y
        self.strength = strength
        if self.x_speed < 0:
            self.reversex = True
        if self.y_speed < 0:
            self.reversey = True
    
    def changeplace(self, x, y):
        self.x = x
        self.y = y
    
    def update(self):
        self.count = 1
        self.create()
        for row in self.icles:
            self.count += 1
        if self.icles[0][3] <= 0:
            self.destroy()
            self.count -= 1;
        for a in range(0, self.count-1):
            self.icles[a][3] -= 1
            self.icles[a][0] += math.cos(self.icles[a][2])
            self.icles[a][1] += math.sin(self.icles[a][2])
            #self.strength = 1 - math.sqrt( ((self.x - self.blowto[0])**2 + (self.y- self.blowto[1])**2)) / 100 
            self.icles[a][0] += self.strength * self.x_speed
            self.icles[a][1] += self.strength * self.y_speed
            
            if a == 0:
                self.xtremes=[int(math.floor(self.icles[a][0])), int(math.floor(self.icles[a][0]))]
                self.ytremes=[int(math.floor(self.icles[a][1])), int(math.floor(self.icles[a][1]))]
            else:
                if self.icles[a][0] < self.xtremes[0]:
                    self.xtremes[0] = int(math.floor(self.icles[a][0]))
                elif self.icles[a][0] > self.xtremes[1]:
                    self.xtremes[1] = int(math.ceil(self.icles[a][0]))
                    
                if self.icles[a][1] < self.ytremes[0]:
                    self.ytremes[0] = int(math.floor(self.icles[a][1]))
                elif self.icles[a][1] > self.ytremes[1]:
                    self.ytremes[1] = int(math.ceil(self.icles[a][1]))
            
            #end with absolute positions of each, is easier? :/

    def draw(self):
        self.width = self.xtremes[1] - self.xtremes[0]
        if self.width <0:
            self.width = self.width - self.width - self.width
        self.height = self.ytremes[1] - self.ytremes[0]
        if self.height <0:
            self.height = self.height - self.height - self.height
        
        self.width += 20  #width of texture
        self.height += 20 #height, assuiming it's topleft.
        
        if self.width > 400:
            self.width = 400
        if self.height > 500:
            self.height = 500
        
        #print self.xtremes[0], self.xtremes[1], self.ytremes[0], self.ytremes[1], self.width, self.height
        self.surface = pygame.Surface((self.width, self.height)).convert_alpha()
        self.surface.fill((255,0,0,100))
        
        for a in range (0, self.count-1):
            if self.reversex == True:
                xf = self.icles[a][0] - self.x + self.width-20
            else:
                xf = self.icles[a][0] - self.x
            if self.reversey == True:
                yf = self.icles[a][1]- self.ytremes[0]
            else:
                yf = self.icles[a][1]- self.y
            self.surface.blit(self.iclesf[ self.icles[a][4] ], (xf, yf))
            #print self.icles[a][0]- self.x + self.width/2, self.icles[a][1] + self.height
        
        return self.surface, pygame.Rect((self.xtremes[0], self.ytremes[0]), (self.width, self.height))